package com.hln.jingwushi.ui

import android.os.Bundle
import android.util.Log
import com.hln.jingwushi.MyBaseActivity
import com.hln.jingwushi.databinding.ActivityEnvironmentalDetectionBinding
import com.hln.jingwushi.utils.DataCacheUtil
import com.gdsz.libbasic.utils.assist.getCurrentDataLong
import com.gdsz.libbasic.utils.assist.getWeek
import com.gdsz.libbasic.utils.assist.getYmd
import com.gdsz.libbasic.utils.assist.weekArray
import com.zgkxzx.modbus4And.requset.ModbusReq
import com.zgkxzx.modbus4And.requset.OnRequestBack

class EnvironmentalDetectionActivity : MyBaseActivity<ActivityEnvironmentalDetectionBinding>() {
    override fun ActivityEnvironmentalDetectionBinding.initBinding(savedInstanceState: Bundle?) {
        readInputRegistersClickEvent()
        val currentDate = getCurrentDataLong()
        binding.tvDate.text = getYmd(currentDate)  + "   ${weekArray[getWeek(currentDate)]}"
        binding.tvName.text = DataCacheUtil.getName()
        binding.tvTip.text = "方舱运行状态正常，紧急联系电话：${DataCacheUtil.getContactTel()}，紧急联系人：${DataCacheUtil.getContactPerson()}"
    }

    override fun onClick() {
        super.onClick()
    }


    fun readInputRegistersClickEvent() {
        ModbusReq.getInstance().readInputRegisters(object : OnRequestBack<ShortArray> {
            override fun onSuccess(data: ShortArray) {
                runOnUiThread {
                    binding.tvTemperatureValue.text = "${data[0]} ℃"
                    binding.tvHumidityValue.text = "${data[1]} %"
                    binding.tvTemp.text = "温度 ${data[0]} ℃  湿度 ${data[1]} %"
                }
                Log.d(
                    "asd",
                    "readInputRegisters onSuccess ${data[0].toInt()} , ${data[1].toInt()}, ${data[2].toInt()} "
                )
            }

            override fun onFailed(msg: String) {
                Log.e("asd", "readInputRegisters onFailed $msg")
            }
        }, 1, 100, 2)
    }

    override fun notifyData(values: ShortArray) {
        runOnUiThread {
            binding.tvTemperatureValue.text = "${values[0]} ℃"
            binding.tvHumidityValue.text = "${values[1]} %"
            binding.tvTemp.text = "温度 ${values[0]} ℃  湿度 ${values[1]} %"
        }
    }
}